<template>
<div>
  <el-row>
    <el-col :span="1"><div class="grid-content bg-purple"></div></el-col>
    <el-col :span="22">
      <div class="grid-content bg-purple-light">
        <br>
        <el-button type="primary" @click="dialogFormVisible = true">打开表单</el-button>
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          :cell-style="rowClass"
          :header-cell-style="headClass" >

          <el-table-column
            prop="secondParty"
            label="租客"
            width="180">
          </el-table-column>

          <el-table-column
            prop="firstParty"
            label="房东"
            width="180">
          </el-table-column>

          <el-table-column
            prop="yearPrice"
            label="1年价格"
            width="180">
          </el-table-column>

          <el-table-column
            prop="beginTime"
            label="开始日期"
            width="180">
          </el-table-column>

          <el-table-column
            prop="endTime"
            label="结束日期"
            width="180">
          </el-table-column>

          <el-table-column
            prop="address"
            label="房屋地址">
          </el-table-column>

          <el-table-column
            label="操作">
            <template slot-scope="scope">

              <el-button type="text" v-if="scope.row.gen" @click="gen(scope.row)">生成合同</el-button>

              <el-link type="success" v-else :underline="false" target="_blank" :href="scope.row.pdfUrl">预览</el-link>
            </template>
          </el-table-column>

        </el-table>
      </div>
    </el-col>
    <el-col :span="2"><div class="grid-content bg-purple"></div></el-col>
  </el-row>



    <el-dialog title="身份信息" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="活动名称" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="http://localhost/dev-api/finance/genConstruct/idCardMsg"
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="form.idCardImg" :src="form.idCardImg" class="avatar" alt="">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="姓名">
          <el-input v-model="form.name" disabled></el-input>
        </el-form-item>

        <el-form-item label="身份证号">
          <el-input v-model="form.idCardNumber" disabled></el-input>
        </el-form-item>

        <el-form-item label="性别">
          <el-input v-model="form.gender" disabled></el-input>
        </el-form-item>

        <el-form-item label="生日">
          <el-input v-model="form.birthday" disabled></el-input>
        </el-form-item>

        <el-form-item label="住址">
          <el-input v-model="form.address" disabled></el-input>
        </el-form-item>

        <el-form-item label="民族">
          <el-input v-model="form.race" disabled></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
  </el-dialog>


</div>
</template>

<script>
import {
genConstruct
} from "@/api/finance/factoring";


  export default {
    name: "index",
    data() {
      return {
        tableData: [{
          firstParty: '王小虎',
          firstPartyIdCard: '1234567890',
          secondParty: '李二',
          secondPartyIdCard: '9876543210',
          address: '长安城大明宫',
          termYear: '3',
          beginTime: '2020-10-1',
          endTime: '3030-10-1',
          yearPrice: 100000.00,
          firstTel: '1008611',
          secondTel: '10010',
          gen: true,
          pdfUrl: '',// pdf 路径
        }],
        dialogFormVisible: false,
        form: {

          idCardImg: '',
          name: null,
          idCardNumber: null,
          race: null,
          birthday: null,
          address: null,
          gender: null,
        },
        formLabelWidth: '120px'
      }
    },
    methods: {
      // 表头样式设置
      headClass () {
        return 'text-align: center;background:rgb(242,242,242);color:rgb(140,138,140)'
      },
      // 表格样式设置
      rowClass () {
        return 'text-align: center;'
      },
      gen(row){

        genConstruct(row).then(response => {

          if (response.code === 200){

            this.tableData[this.tableData.indexOf(row)].gen = false;
            this.tableData[this.tableData.indexOf(row)].pdfUrl = response.data;

            /*
              minio
             */
            // this.tableData[this.tableData.indexOf(row)].pdfUrl = response.data.data.url;
            console.log(this.tableData[this.tableData.indexOf(row)].pdfUrl)
          }
        });
      },
      handleAvatarSuccess(res, file) {

        console.log(res)
        this.form.idCardImg = res.upload.url;
        console.log(res.upload.url)

        let card = res.idMsg.cards[0];
        this.form.name = card.name;
        this.form.idCardNumber = card.id_card_number;
        this.form.race = card.race;
        this.form.birthday = card.birthday;
        this.form.address = card.address;
        this.form.gender = card.gender;
      },
    }
  }
</script>

<style scoped>
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
